var username = document.querySelector('.username');
var password = document.querySelector('.pwd');
var submit = document.querySelector('.sub');
var ajaxObj = {
    createXhr: function () {
        var xhrObj = window.XMLHttpRequest ? new XMLHttpRequest : new ActiveXObject('Microsoft.XMLHTTP');
        return xhrObj;
    },
    formData: function (data) {
        var formdata = '';  
        for ( var i in data) {  
            formdata += i + '=' + data[i] + '&';  
        }  
        return formdata;
    },
    Get: function (xhr,target,callback,string) {
        xhr.onreadystatechange = function() {
            if (xhr.readyState == 4) {
                if (xhr.status == 200) {
                    callback(xhr.responseText);
                } else {
                    return;
                }
            }
        }
        if (string) {
            xhr.open("GET",target + "?" + string,true);
        } else {
            xhr.open("GET",target,true);
        }
        xhr.send(null);
    },
    Post: function (xhr,target,callback,string) {
        xhr.onreadystatechange = function() {  
            if (xhr.readyState == 4 && xhr.status == 200) {  
                callback.call(this, xhr.responseText);  
            }  
        }  
        xhr.open('POST', target, true);  
        // 这句比较重要  
        xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');  
        xhr.send(string);  
    }
}
submit.addEventListener("click", function (e) {
    var xhrObj = ajaxObj.createXhr();
    var info = ajaxObj.formData({
        'username': username.value,
        'password': password.value
    });
    ajaxObj.Post(xhrObj, './login', function (res) {

        var res_obj = JSON.parse(res);
        console.log(res_obj);
        if (res_obj.err === 0) {
            window.location.href = './';//跨域问题，想清楚了，cookie跨域不支持，192.168.1.149和localhost是不同的域
        }
    }, info)

})